package com.gkzf.ai.module.crm.util.assembly;

import com.alibaba.fastjson.JSONArray;
import com.gkzf.ai.module.crm.controller.admin.creditReport.vo.creditreportdetails.CreditReportDetailsSaveReqVO;
import com.gkzf.ai.module.crm.dal.dataobject.creditReport.UserCreditReportAdmittanceConditionDO;
import com.gkzf.ai.module.crm.util.packaging.CalculationQueryRecord;
import com.gkzf.ai.module.system.api.dict.dto.DictDataRespDTO;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AssembleQueryRecordDetails {
    // 报告中关于查询记录部分的键 (key)
    public static final String REPORT_KEY_INSTITUTION_QUERY_RECORDS_DETAILS = "机构查询记录明细";

    public static final String REPORT_KEY_SELF_QUERY_RECORDS_DETAILS = "本人查询记录明细";

    private List<DictDataRespDTO> queryLimitDictDataList;

    public AssembleQueryRecordDetails(List<DictDataRespDTO> queryLimitDictDataList) {
        this.queryLimitDictDataList = queryLimitDictDataList;
    }

    public void getAssembleQueryRecordDetails(Map<String, Object> param, UserCreditReportAdmittanceConditionDO conditionDO, CreditReportDetailsSaveReqVO detailsDO) {
        List<Map<String, String>> list = new ArrayList<>();
        if (param.containsKey(REPORT_KEY_INSTITUTION_QUERY_RECORDS_DETAILS)){
            Object o = param.get(REPORT_KEY_INSTITUTION_QUERY_RECORDS_DETAILS);
            if(o instanceof JSONArray jsonArray){
                for (Object obj : jsonArray) {
                    Map<String, String> map = (Map<String, String>) obj;
                    list.add(map);
                }

            }

        }
        if (param.containsKey(REPORT_KEY_SELF_QUERY_RECORDS_DETAILS)){
            Object o = param.get(REPORT_KEY_SELF_QUERY_RECORDS_DETAILS);
            if(o instanceof JSONArray jsonArray){
                for (Object obj : jsonArray) {
                    Map<String, String> map = (Map<String, String>) obj;
                    list.add(map);
                }

            }

        }
        CalculationQueryRecord calculationQueryRecord = new CalculationQueryRecord(queryLimitDictDataList);
        String extractQueryRecord = calculationQueryRecord.extractQueryRecord(list, detailsDO);
        conditionDO.setCreditReportQueryJson(extractQueryRecord);
    }

}
